package com.mtx.weatherreport.bean;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @author yinghuayu
 * @create 2018/2/26.
 */

public class CityDB {
    public static final String CITY_DB_NAME = "city.db";
    private static final String CITY_TABLE_NAME = "city";
    private SQLiteDatabase db;

    public CityDB(Context context, File file) {
        // 如果存在数据库，打开；如果不存在，建立一个新的数据库
        db = SQLiteDatabase.openOrCreateDatabase(file, null);  // 路径为path+/CITY_DB_NAME
    }

    public List<City> getAllCity() {
        List<City> list = new ArrayList<>();

        // 直接调用select语句
        Cursor cursor = db.rawQuery("SELECT * from " + CITY_TABLE_NAME,null);
        while(cursor.moveToNext()) {
            String province = cursor.getString(cursor.getColumnIndex("province"));
            String city = cursor.getString(cursor.getColumnIndex("city"));
            String number = cursor.getString(cursor.getColumnIndex("number"));
            String allPy = cursor.getString(cursor.getColumnIndex("allpy"));
            String allFirstPy = cursor.getString(cursor.getColumnIndex("allfirstpy"));
            String firstPy = cursor.getString(cursor.getColumnIndex("firstpy"));

            City item = new City(province,city,number,firstPy,allPy,allFirstPy);
            list.add(item);
        }
        return list;
    }
}
